from crawler.csdn_crawler import CsdnCrawler
from strategy import pv_strategy
from strategy import time_strategy
import datetime
from util.log import logger, init_loguru_logger
import time
import random
from util import file
from util import config
from util import email

hour_pv_file_path = "./log/hour_pv.log"
config_file_path = "./conf/config.toml"
# TODO:
# 1) 使用Redis存储昨天pv
# 2) 使用Redis
# 3) 在run.sh中管理进程号, 实现control.sh的功能

@logger.catch
def increase_csdn_pv():
    logger.info("start to increase page view...")
    daily_visit_num = 0
    daily_succ_num = 0
    while True:
        hourly_visit_num = 0
        hourly_succ_num = 0
        sleeptime = time_strategy.get_sleeptime_by_hour()
        hour = datetime.datetime.now().strftime('%H')

        # record the daily pv growth of yesterday to file and send email at 0 o'clock
        if hour == '00':
            total_pv = CsdnCrawler().get_total_pv()
            logger.info("yesterday pv growth, crawler-succ:{}, crawler-fail:{}, total:{}",
                        daily_succ_num, daily_visit_num - daily_succ_num, total_pv)
            daily_pv_msg = "yesterday pv growth, crawler-succ:{}, crawler-fail:{}, total:{}\n".format(
                daily_succ_num, daily_visit_num - daily_succ_num, total_pv
            )
            file.write2file(hour_pv_file_path, daily_pv_msg)
            # send email
            send_email(subject="yesterday csdn pv growth", content=daily_pv_msg)
            # reset daily data
            daily_visit_num = 0
            daily_succ_num = 0
        else:
            logger.info("DEBUG::hour:{}", hour)

        while True:
            # count the total_visit_num and succ_num
            hourly_visit_num += 1
            target_url = pv_strategy.get_random_url_arrcording_to_pv(url_with_prob)
            response = CsdnCrawler().visit_csdn(target_url)
            if response is not None:
                hourly_succ_num += 1
            else:
                logger.warning("visit {} fail", target_url)
            pv_log = "\rhour: {}, the overall progress of succ_num/total_visit_num: {}/{}".format(
                hour, hourly_succ_num, hourly_visit_num)
            print(pv_log, end="")

            time.sleep(random.random() * sleeptime)
            # if running into next hour, then quit and get the new sleeptime
            if datetime.datetime.now().strftime('%H') != hour:
                print("\n", end="")
                logger.info("hour {} growth, succ:{}, fail:{}, total:{}",
                            hour, hourly_succ_num, hourly_visit_num-hourly_succ_num, hourly_visit_num)
                pv_growth = "date:{} hour:{} succ:{} fail:{} total:{}\n".format(
                    time.strftime("%Y%m%d"), hour, hourly_succ_num, hourly_visit_num-hourly_succ_num, hourly_visit_num)
                file.write2file(hour_pv_file_path, pv_growth)
                daily_visit_num += hourly_visit_num
                daily_succ_num += hourly_succ_num
                break


@logger.catch
def send_email(subject, content):
    email.send_email(
        sender=cfg['email']['sender_email'],
        pwd=cfg['email']['sender_password'],
        smtp_host="smtp.qq.com",
        receiver=cfg['email']['receiver_email'],
        subject=subject,
        contents=content)
    return


if __name__ == "__main__":
    # 0) init logger & config
    init_loguru_logger(level="INFO")
    logger.info("init logger successfully")
    cfg = config.load_config_from_file(file_path=config_file_path)

    # 1) get article info: article url and read_num
    logger.info("start to get the article infos from csdn...")
    article_info = CsdnCrawler("https://blog.csdn.net/TOMOCAT").get_article_info()
    logger.info("get all ariticle successfully, article infos:[{}]", article_info)

    # 2) use pv strategy to get url's probability
    logger.info("generate the urls with probability...")
    url_with_prob = pv_strategy.probability_distribution_url(article_info)
    logger.info("url_with_prob:{}", url_with_prob)

    # 3) main work
    try:
        increase_csdn_pv()
    except Exception as e:
        logger.exception(e)
        send_email("pv refresher quit with unknown error", str(e))
